package cn.edu.lingnan.controller;

import cn.edu.lingnan.bean.Book;
import cn.edu.lingnan.service.book.BookService;
import cn.edu.lingnan.tools.ResultVo;
import cn.edu.lingnan.tools.ResultVoUtils;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    //列表查询
    @GetMapping("/listBook")
    @ResponseBody
    //@RequestParam(defaultValue = "1",name = "pageNum")  对参数给默认值
    public ResultVo listBook(@RequestParam(defaultValue = "1", name = "page") String pageNum,
                             @RequestParam(defaultValue = "10", name = "limit") String pageSize,
                             String strName) {
        Map<String, Object> objectMap = bookService.page(Integer.parseInt(pageNum), Integer.parseInt(pageSize),strName);
        //总记录数
        Long count = (Long) objectMap.get("TotalElements");
        List<Book> bookTestList = (List<Book>) objectMap.get("pageContent");

        //  return new ResultVo(0,"列表分页查询",bookTestList,"基于Layui 动态表格数据渲染",count);
        return ResultVoUtils.success("列表分页查询", bookTestList, "基于Layui 动态表格数据渲染", count);
    }

    /**
     * @ResponseBody
     *      将后端以return返回的Java备案类型数据转换为Json数据格式
     *
     * @RequestBody
     *      将前段传递过来的JSON格式的数据转换成为自定义的javabean类型
     * 常见的错误类型：
     *  404：请求路径不对url，找不到映射资源
     *  500：服务器错误，请求到路径，业务逻辑有误
     *  405：请求方式不匹配（get/post请求方式不对）
     */
    @DeleteMapping("/deleteById")
    @ResponseBody
    public ResultVo deleteById(@RequestBody JSONObject jsonObject) {

        int id = Integer.parseInt(jsonObject.getStr("id"));

        System.out.println(id);
        bookService.deleteById(id);

        return ResultVoUtils.success("删除成功", "列表数据行删除操作");
    }

    @DeleteMapping("/deleteBatch")
    @ResponseBody
    public ResultVo deleteBatch(@RequestBody String[] ids){

        System.out.println(Arrays.toString(ids)+"==========dis");

        bookService.deleteBatch(ids);
        return ResultVoUtils.success("批量删除成功", "列表数据行批量删除操作");
    }

    @GetMapping("/showInfo")
    public String showInfo(){
        return "/bookpage/showInfo";
    }

    @GetMapping("addOrUpdateInfo")
    public String addOrUpdateInfo(){
        return "/bookpage/addOrUpdateInfo";
    }

    @PostMapping("/addOrUpdateInfosubmit")
    @ResponseBody
    public String addOrUpdateInfoSubmit( @RequestBody JSONObject jsonObject){

        System.out.println("跑饿了没  "+ jsonObject);

        String name = jsonObject.getStr("name");
        String author = jsonObject.getStr("author");
        int price = Integer.parseInt(jsonObject.getStr("price")) ;
        int sales = Integer.parseInt(jsonObject.getStr("sales"));
        int stocks = Integer.parseInt(jsonObject.getStr("stocks"));
        String imgPath = jsonObject.getStr("imgPath");
        String createTime = jsonObject.getStr("createTime");

        Book book = new Book();
        book.setName(name);
        book.setAuthor(author);
        book.setPrice(price);
        book.setSales(sales);
        book.setStocks(stocks);
        book.setImgPath(imgPath);
        book.setCreateTime(createTime);

        System.out.println(book+"------打印保存对象");

        bookService.save(book);

        return "bookpage/tableDate02";
    }

    @PostMapping("/saveBean")
    @ResponseBody
    public ResultVo saveBean(@RequestBody Book book){

        System.out.println(book+"-------------book传参");

        if(ObjectUtil.isNotNull(book)){
            Book bookVo = bookService.save(book);
            if (ObjectUtil.isNotNull(bookVo)){
                return ResultVoUtils.success("执行成功","列表添加操作");
            }else {
                return ResultVoUtils.success("执行失败","列表添加操作");
            }
        }

        return null;

    }


    @PostMapping("/editBean")
    @ResponseBody
    public ResultVo editBean(@RequestBody Book book){

        System.out.println(book+"-------------book传参");

        if(ObjectUtil.isNotNull(book)){
            //bookService.save()  如果传入的实体类中有id,则作修改操作，如没有，做添加操作
            Book bookVo = bookService.save(book);
            //自定义修改
            //Book bookVo = bookService.update(book,book.getId());
          //  Book bookVo = bookService.save(book);
            if (ObjectUtil.isNotNull(bookVo)){
                return ResultVoUtils.success("执行成功","列表修改操作");
            }else {
                return ResultVoUtils.success("执行失败","列表修改操作");
            }
        }

        return null;

    }

    @GetMapping("/login")
    public String login(){
        return "/bookpage/login-3";
    }

    @GetMapping("/index")
    public String index(){
        return "/index";
    }


}
